package com.dckj.auto.common.utils;


import com.dckj.auto.common.utils.userauto.MenuTree;

import java.util.ArrayList;
import java.util.List;

public class MenuTreeUtil {

    public static MenuTree getRootNode(List<MenuTree> menuTrees, MenuTree rootNode){
        for (int i= 0 ; i<menuTrees.size(); i++){
            MenuTree menuTree = menuTrees.get(i);
            searchChildNode(rootNode,menuTree);

        }
        return rootNode;
    }

    public static boolean searchChildNode(MenuTree searchNode,MenuTree currentNode){
        if (searchNode.getId().equals(currentNode.getPid())){
            List<MenuTree> children = searchNode.getChildren();
            if(children==null){
                children  = new ArrayList<>();
            }
            children.add(currentNode);
            searchNode.setChildren(children);
            return true;
        }else{
            if(null == searchNode.getChildren() || searchNode.getChildren().size() == 0  ){
                return false;
            }else{
                for (int i= 0 ; i<searchNode.getChildren().size(); i++) {
                    MenuTree menuTree = searchNode.getChildren().get(i);
                    if (searchChildNode(menuTree,currentNode)){
                        return true;
                    }
                }
                return false;
            }
        }
    }
    


}
